package com.teamtgv.shareshark.result;

import java.math.BigInteger;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.teamtgv.shareshark.ShareActivity;
import com.teamtgv.shareshark.UserData;
import com.teamtgv.shareshark.task.DownvoteRecommendationTask;
import com.teamtgv.shareshark.task.UndoDownvoteTask;
import com.teamtgv.shareshark.task.UndoUpvoteTask;
import com.teamtgv.shareshark.task.UpvoteRecommendationTask;

/**
 * This class is Parcelable because it is passed as data from the ResultList to the ResultView.
 * Apparently android likes Parcelable more then Serializable
 */
public class Result implements Parcelable {

	private int databaseId;
	private String by; // recommender
	private String to; // target
	private String what;// the subject of the share
	private String where;// the location of the share
	private String dateRecommended;// the date the share was recommended
	private int rating_plus; // current rating
	private int rating_min;
	private String imageUrl = ""; // imageshack url or empty if no photo
	
	private boolean downvoted = false;
	private boolean upvoted = false;
	
	public Result(int id, BigInteger recommenderFacebookId, BigInteger targetFacebookId, String what, String where, String date, int rating_plus, int rating_min, String photoUrl){
		String recommenderName = UserData.getName(recommenderFacebookId);
        Log.v("Result", "Resolved recommender " + recommenderFacebookId + " to name " + recommenderName);
        String targetName;
        if (targetFacebookId.compareTo(new BigInteger("-1")) == 0) {
        	targetName = ShareActivity.EVERYONE;
        } else {
        	targetName = UserData.getName(targetFacebookId);
        	Log.v("Result", "Resolved target " + targetFacebookId + " to name " + targetName);
        }
        this.databaseId = id;
		this.by = recommenderName;
		this.to = targetName;
		this.what = what;
		this.where = where;
		this.dateRecommended = date;
		this.rating_plus = rating_plus;
		this.rating_min = rating_min;
		this.imageUrl = photoUrl;
	}
	
public Result(int id, String by, String to, String what, String where, String date, int rating_plus, int rating_min, String photoUrl){
		this.databaseId = id;
		this.by = by;
		this.to = to;
		this.what = what;
		this.where = where;
		this.dateRecommended = date;
		this.rating_plus = rating_plus;
		this.rating_min = rating_min;
		this.imageUrl = photoUrl;
	}
	
	public String getRecommender() {
		return by;
	}
	
	public String getTarget() {
		return to;
	}
	
	public boolean hasPhoto() {
		return (imageUrl != null) && (imageUrl.length() > 0) && (! imageUrl.equalsIgnoreCase("null"));
	}
	
	public String getPhotoUrl() {
		return imageUrl;
	}

	public String getSubject() {
		return what;
	}

	public String getLocation() {
		return where;
	}
	
	public String getDistance() {
		return "2,5km";
	}

	public String geTimeStampRecommended() {
		return dateRecommended;
	}
	
	public String getDateRecommended() {
		String[] dateTimeParts = dateRecommended.split(" ");
		if (dateTimeParts.length < 2)
			return dateRecommended;
		String[] dateParts = dateTimeParts[0].split("-");
		if (dateParts.length != 3)
			return dateTimeParts[0];
		return dateParts[2] + "-" + dateParts[1] + "-" + dateParts[0];
	}
	
	public int getPlusRating() {
		return rating_plus;
	}
	
	public int getMinRating() {
		return rating_min;
	}
	
	public boolean isUpvoted() {
		return upvoted;
	}
	
	public boolean isDownvoted() {
		return downvoted;
	}
	
	public void increaseRating() {
		upvoted = true;
		UpvoteRecommendationTask upvoteTask = new UpvoteRecommendationTask(databaseId);
		upvoteTask.execute();
	}
	
	public void undoIncreaseRating() {
		upvoted = false;
		UndoUpvoteTask undoTask = new UndoUpvoteTask(databaseId);
		undoTask.execute();
	}
	
	public void decreaseRating() {
		downvoted = true;
		DownvoteRecommendationTask downvoteTask = new DownvoteRecommendationTask(databaseId);
		downvoteTask.execute();
	}
	
	public void undoDecreaseRating() {
		downvoted = false;
		UndoDownvoteTask undoTask = new UndoDownvoteTask(databaseId);
		undoTask.execute();
	}

	@Override
	public int describeContents() {
		return 0;
	}
	
	@Override
	public boolean equals(Object o) {
		if (o == null || ! (o instanceof Result))
			return false;
		Result other = (Result) o;
		return this.what.equals(other.what) &&
				this.where.equals(other.where) &&
				this.dateRecommended.equals(other.dateRecommended);
	}

	/**
	 * Method used to write the content of this class to a parcelable object.
	 * This is automatically used in the following code from the ResultListActivity:
	 * myIntent.putExtra(ResultViewActivity.RESULT_OBJECT, item);
	 */
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(databaseId);
		dest.writeString(by);
		dest.writeString(to);
		dest.writeString(what);
		dest.writeString(where);
		dest.writeString(dateRecommended);
		dest.writeInt(rating_plus);
		dest.writeInt(rating_min);
		dest.writeString(imageUrl);
	}
	
	/**
	 * A creator used to recreate the original Result object from a Parcelable
	 * This is automatically used in the following code from the ResultViewActivity:
	 * Result result = (Result) getIntent().getParcelableExtra(RESULT_OBJECT); 
	 */
	public static final Parcelable.Creator<Result> CREATOR =
		    	new Parcelable.Creator<Result>() {

					@Override
					public Result createFromParcel(Parcel source) {
						int id = source.readInt();
						String by = source.readString();
						String to = source.readString();
						String what = source.readString();
						String where = source.readString();
						String date = source.readString();
						int rating_plus = source.readInt();
						int rating_min = source.readInt();
						String imageUrl = source.readString();
						return new Result(id, by, to, what, where, date, rating_plus, rating_min, imageUrl);
					}

					@Override
					public Result[] newArray(int size) {
						return new Result[size];
					}
		 
	 };
	
}
